package com.example.contactfragment;

import java.io.File;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.boyuan.huijiaoyu.R;
import com.example.newfragment.DetailPerson;
import com.magicsoft.app.adapter.CTHAdapter;
import com.magicsoft.constant.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.utils.StorageUtils;


/**
 * ViewPager数据适配器
 */

public class HomeContactAdapter extends CTHAdapter<DetailPerson> {

	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	/**
     * 
     */
	private int mRightWidth = 0;

	/**
	 * 单击事件监听器
	 */
	private IOnItemRightClickListener mListener = null;

	public interface IOnItemRightClickListener {
		void onRightClick(View v, int position);
	}

	public HomeContactAdapter(Context context,
			List<DetailPerson> data, int rightWidth,
			IOnItemRightClickListener l) {
		// TODO Auto-generated constructor stub
		super(context, data);
		mRightWidth = rightWidth;
		mListener = l;
		// this.options = options;
		// this.imageLoader = imageLoader;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		final int thisPosition = position;
		DetailPerson contactEntity = data.get(position);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.home_contact_common_item, null);
			holder.item_left = (View) convertView.findViewById(R.id.item_left);
			holder.item_right = (View) convertView
					.findViewById(R.id.item_right);

			holder.item_delete = (TextView) convertView
					.findViewById(R.id.item_delete);

			holder.imageView = (ImageView) convertView
					.findViewById(R.id.imageview101);
			holder.iv_favorite = (ImageView) convertView
					.findViewById(R.id.iv_favorite);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_job = (TextView) convertView.findViewById(R.id.tv_job);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		LinearLayout.LayoutParams lp1 = new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		holder.item_left.setLayoutParams(lp1);
		LinearLayout.LayoutParams lp2 = new LayoutParams(mRightWidth,
				LayoutParams.MATCH_PARENT);
		holder.item_right.setLayoutParams(lp2);

		holder.item_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onRightClick(v, thisPosition);
				}
			}
		});

//		if (StringUtils.isImageLayout(contactEntity.getAvatar())) {
//			imageLoader.displayImage(contactEntity.getAvatar(),
//					holder.imageView);
//		}

		if (contactEntity.getIs_favorite().equals("1")) {
			holder.iv_favorite.setVisibility(View.INVISIBLE);
		} else{
			holder.iv_favorite.setVisibility(View.INVISIBLE);
		}
		
		
		holder.tv_name.setText(contactEntity.getRealname());
		holder.tv_job.setText(contactEntity.getFamily_name());
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_header)
		.showImageForEmptyUri(R.drawable.default_header)
		.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
		.build();
//		imageLoader.clearMemoryCache();
//		imageLoader.clearDiskCache();
		String uri = Constant.photourl + contactEntity.getMobile();
		imageLoader.displayImage(uri, holder.imageView);
		return convertView;
	}

	static class ViewHolder {
		View item_left;
		View item_right;
		TextView item_delete;
		ImageView imageView;
		ImageView iv_favorite;
		TextView tv_name;
		TextView tv_job;
	}
}
